package util;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.util.Map;
import models.Message;
import models.Notification;
import models.Photo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonObject;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;
import play.mvc.Http.Cookie;
import play.mvc.Http.Response;
import services.image.ImageSearchService.ImageExt;
import services.video.VideoFeed;
import controllers.BaseController;
import controllers.Data;

public class JsonParserHelper {
	
	
	
	public static Photo toPhoto(byte[] content) {
		String sContent = new String(content);
		InputStream is = new ByteArrayInputStream(content);
		Reader reader = new InputStreamReader(is);
		Type dataType = new TypeToken<Photo>(){}.getType();
		return BaseController.gson().fromJson(reader, dataType);
	}
	
	public static Data<Photo> toPhotoCollection(byte[] content) {
		String sContent = new String(content);
		InputStream is = new ByteArrayInputStream(content);
		Reader reader = new InputStreamReader(is);
		Type dataType = new TypeToken<Data<Photo>>(){}.getType();
		return BaseController.gson().fromJson(reader, dataType); 
	}
	
	public static Data<ImageExt> toImageCollection(byte[] content) {
		InputStream is = new ByteArrayInputStream(content);
		Reader reader = new InputStreamReader(is);
		Type dataType = new TypeToken<Data<ImageExt>>(){}.getType();
		return BaseController.gson().fromJson(reader, dataType); 
	}
	
	public static VideoFeed toVideoFeed(byte[] content) {
		InputStream is = new ByteArrayInputStream(content);
		Reader reader = new InputStreamReader(is);
		Type dataType = new TypeToken<VideoFeed>(){}.getType();
		return BaseController.gson().fromJson(reader, dataType);
	}
	
	public static Data<Message> toMessageCollection(byte[] content) {
		InputStream is = new ByteArrayInputStream(content);
		Reader reader = new InputStreamReader(is);
		Type dataType = new TypeToken<Data<Message>>(){}.getType();
		return BaseController.gson().fromJson(reader, dataType);
	}
	
	public static Data<Notification> toNotificationCollection(byte[] content) {
		InputStream is = new ByteArrayInputStream(content);
		Reader reader = new InputStreamReader(is);
		Type dataType = new TypeToken<Data<Notification>>(){}.getType();
		return BaseController.gson().fromJson(reader, dataType);
	}
	
	public static Message toMessage(byte[] content) {
		InputStream is = new ByteArrayInputStream(content);
		Reader reader = new InputStreamReader(is);
		Type dataType = new TypeToken<Message>(){}.getType();
		return BaseController.gson().fromJson(reader, dataType);
	}
	
	public static JsonArray toJsonArray(byte[] content) {
		InputStream is = new ByteArrayInputStream(content);
		Reader reader = new InputStreamReader(is);
		JsonElement jsobj = new JsonParser().parse(reader);
		return jsobj.getAsJsonArray();
	}
	
	public static JsonObject toJsonObject(byte[] content) {
		InputStream is = new ByteArrayInputStream(content);
		Reader reader = new InputStreamReader(is);
		JsonElement jsobj = new JsonParser().parse(reader);
		return jsobj.getAsJsonObject();
	}
	
	
}